/*
 *
 * Saturn Framework Co.Limt
 * Copyright (c) 2012-2019 tanks, Inc. All rights reserved.
 */
 
package com.saturn.config.server.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.saturn.config.server.base.entity.AppInfo;
import com.saturn.config.server.base.entity.AppVersion;
import com.saturn.config.server.base.mapper.AppConfigMapper;
import com.saturn.config.server.base.mapper.AppVersionMapper;
import com.saturn.config.server.base.mapper.ConfigLogMapper;
import com.saturn.config.server.service.AppVersionService;

/**
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	none
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>配置历史版本
 *    </dd>
 *
 * @author tanks
 * @date 2019-07-08 15:58:42
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

	@Autowired
	private AppConfigMapper appConfigMapper;
	@Autowired
	private ConfigLogMapper configLogMapper;
	
	@Override
	public IPage<AppInfo> queryPage(IPage<AppInfo> page, Map<String,Object> params) {
		return this.baseMapper.queryPage(page, params);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public int deleteAppVersion(String[] ids) {
		if( ids==null || ids.length==0 ) return  0;
		int count = 0;
		for(String id: ids) {
			AppVersion appVersion = this.baseMapper.selectById(id);
			if( appVersion==null ) continue;
			QueryWrapper queryWrapper = Wrappers.query();
			queryWrapper.eq("app_id", appVersion.getAppId());
			queryWrapper.eq("version", appVersion.getVersion());
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("appId", appVersion.getAppId());
			params.put("version", appVersion.getVersion());
			//删除对应的版本日志信息
			this.configLogMapper.deleteByVersion(params);
			//删除对应的配置信息
			this.appConfigMapper.delete(queryWrapper);
			this.baseMapper.deleteById(id);
			count++;
		}
		return count;
	}

}
